package com.yuezhu.constomview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yuezhu.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zhao on 2016/5/4.
 */
public class AssetView extends LinearLayout {
    ListView listView;
    AssetAdapter adapter;
    OnClickListener onClickListener;

    public AssetView(Context context) {
        super(context);
        initView(context);
    }

    public AssetView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AssetView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_asset_view, this);
        listView = (ListView) view.findViewById(R.id.listview);
        adapter = new AssetAdapter();
        listView.setAdapter(adapter);
    }

    public void setAssetViewListener(OnClickListener clickListener, AdapterView.OnItemClickListener itemClickListener) {
        listView.setOnItemClickListener(itemClickListener);
        onClickListener = clickListener;
    }

    class AssetAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 10;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_asset, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.assetItemText.setText("爱家公寓\n出租：10\n总房数：100");
            holder.assetItemButton.setText("王小虎");
            holder.assetItemButton.setTag(position);
            holder.assetItemButton.setOnClickListener(onClickListener);
            setTitle(position, holder.assetItemTitle);
            return convertView;
        }

        private void setTitle(int position, TextView title) {
            title.setVisibility(VISIBLE);
            switch (position) {

                case 0:
                    title.setText("公寓一");
                    break;
                case 3:
                    title.setText("公寓二");
                    break;
                default:
                    title.setVisibility(GONE);
                    break;
            }
        }

         class ViewHolder {
            @InjectView(R.id.asset_item_title)
            TextView assetItemTitle;
            @InjectView(R.id.asset_item_icon)
            ImageView assetItemIcon;
            @InjectView(R.id.asset_item_text)
            TextView assetItemText;
            @InjectView(R.id.asset_item_button)
            Button assetItemButton;

            ViewHolder(View view) {
                ButterKnife.inject(this, view);
            }
        }
    }
}
